<!DOCTYPE html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<head>
<!--    <base href="<%=basePath%>">-->
    <meta charset="UTF-8">
    <script type="text/javascript " src="../js/jquery-1.11.1-min.js"></script>
    <script type="text/javascript" src="../bootstrap/js/bootstrap.min.js"></script>
    <link href="../bootstrap/css/bootstrap.min.css" type="text/css" rel="stylesheet"/>
    <style type="text/css">
        .content {
            margin-top: 20px;
        }

        .color {
            background-color: #ffffff;
        }

        h3 {
            text-align: center;
        }

        #search {
            float: left;
            margin: 10px;
        }

        #search div {
            margin-right: 5px;
        }

        #add_del {
            float: right;
            margin: 10px;
        }

        #add_del a {
            margin-left: 5px;
        }

        td, th {
            text-align: center;
        }
    </style>
</head>
<script type="text/javascript" th:inline="javascript">
    $(function () {
        // // 注销
        // $(".logoutBtn").click(function () {
        //     $.ajax({
        //         url: "admin/logout",
        //         type: "get",
        //         dataType: "json",
        //         success: function (data) {
        //             if (data.success) {
        //                 window.location.href = "admin/loginPage"
        //                 window.event.returnValue=false;
        //             } else {
        //                 $.alert("注销失败")
        //             }
        //         }
        //     })
        // })
    })

    function getTopicsByCondition() {
        let title = $("#title").val()
        let createBy = $("#createBy").val()
        let createTime = $("#createTime").val()

        window.location.href="/admin/topicList?title="+title+"&createBy="+createBy+"&createTime="+createTime;
    }
    // 根据id删除帖子
    function deleteTopic(id) {
        var msg = "您真的确定要删除吗？\n\n请确认！";
        if(confirm(msg)==true){
            $.ajax({
                url: "/article/deleteTopicById",
                data: {
                    "id": id,
                },
                type: "post",
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        getTopicsByCondition();
                    }
                }
            })
        }

    }
    // 根据id置顶帖子
    function setTop(id) {
        var msg = "您真的确定要置顶吗？\n\n请确认！";
        if (confirm(msg) == true) {
            $.ajax({
                url: "/article/top",
                data: {
                    "id": id,
                },
                type: "post",
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        getTopicsByCondition();
                    }
                }
            })
        }
    }
    // 根据id加精帖子
    function setEssence(id) {
        var msg = "您真的确定要加精吗？\n\n请确认！";
        if (confirm(msg) == true) {
            $.ajax({
                url: "/article/essence",
                data: {
                    "id": id,
                },
                type: "post",
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        getTopicsByCondition();
                    }
                }
            })
        }
    }

    function deleteSelected() {
        var option = $(":checked");
        var checkedId = "";
        var boo = true;
        ids = new Array()

        //拼接除全选框外,所有选中的id,
        for (var i = 0, len = option.length; i < len; i++) {
            if (boo) {
                if (option[i].id == 'firstCb') {
                    boo = true;
                } else {
                    boo = false;
                    checkedId += option[i].value;
                    // ids[i] = option[i].value
                }
            } else {
                checkedId += "," + option[i].value;
                // ids[i] = option[i].value

            }
        }

        if (checkedId == null||checkedId == ""){
            alert("请选择记录")
        }else {
            if(confirm("确认删除吗")==true){
                $.ajax({
                    type:"post",
                    url:'/article/deleteTopicByIds',
                    data : {
                        "ids":checkedId
                    },
                    dataType:"json",
                    success:function (data) {
                        if (data.success){
                            alert("删除成功！")
                            getTopicsByCondition()
                        }else {
                            alert("删除失败！")
                        }

                    }
                })
            }
        }
    }
    function topSelected() {
        var option = $(":checked");
        var checkedId = "";
        var boo = true;
        ids = new Array()

        //拼接除全选框外,所有选中的id,
        for (var i = 0, len = option.length; i < len; i++) {
            if (boo) {
                if (option[i].id == 'firstCb') {
                    boo = true;
                } else {
                    boo = false;
                    checkedId += option[i].value;
                    // ids[i] = option[i].value
                }
            } else {
                checkedId += "," + option[i].value;
                // ids[i] = option[i].value

            }
        }

        if (checkedId == null||checkedId == ""){
            alert("请选择记录")
        }else {
            if(confirm("确认置顶吗吗")==true){
                $.ajax({
                    type:"post",
                    url:'/article/topTopicByIds',
                    data : {
                        "ids":checkedId
                    },
                    dataType:"json",
                    success:function (data) {
                        if (data.success){
                            alert("置顶成功！")
                            getTopicsByCondition()
                        }else {
                            alert("置顶失败！")
                        }

                    }
                })
            }
        }
    }
</script>
<body style="background-color: #F7F8FA;">
<nav class="navbar navbar-default color" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <a class="navbar-brand">BBS</a>
        </div>
        <div class="col-lg-6" style="width: auto;">
            <ul class="nav navbar-nav">
                <li><a href="/admin/userList">用户管理</a></li>
                <li class="active"><a href="/admin/topicList">帖子管理</a></li>
            </ul>
        </div>
        <div class="col-lg-2 col-lg-offset-6"  style="width: 20%">
            <ul class="nav navbar-nav">
                <li th:if="*{session.admin.name != null}"><a th:text="'欢迎你,'+ *{session.admin.name}"
                                                    style="color: red"></a></li>
                <li><a href="/admin/logout" class="logoutBtn">注销</a></li>
            </ul>
        </div>
    </div>

</nav>


<div class="container-fluid content color main" style="width: 80%;">
    <div class="row">
        <div class="col-lg-12 ">
            <h2><span class="glyphicon glyphicon-list" aria-hidden="true"></span> 帖子管理 </h2>
            <hr>
        </div>
        <div class="container">
            <h3>帖子信息列表</h3>
            <form id="search" class="form-inline" action="" method="post">
                <div class="form-group">
                    <label for="exampleInputTitle2">标题</label>
                    <input type="text" name="title" value="" class="form-control" id="title">
                </div>
                <div class="form-group">
                    <label for="exampleInputName3">创建人</label>
                    <input type="text" name="name" value="" class="form-control" id="createBy">
                </div>
                <div class="form-group">
                    <label for="exampleInputTime2">创建时间</label>
                    <input type="text" name="time" value="" class="form-control" id="createTime">
                </div>
                <div class="form-group">
                    <button type="button" class="btn btn-default" onclick="getTopicsByCondition()">查询</button>
                </div>
            </form>

            <div id="add_del">
                <button class="btn btn-primary"  onclick="topSelected()">置顶选中</button>
                <button class="btn btn-primary"  onclick="deleteSelected()">删除选中</button>
            </div>

            <form id="form" action="" method="post">
                <table border="1" class="table table-bordered table-hover">
                    <tr class="success">
                        <th><input type="checkbox" id="firstCb"></th>
                        <th>编号</th>
                        <th>标题</th>
                        <th>创建人</th>
                        <th>创建时间</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
<!--                    <c:forEach items="${topics}" var="topic" varStatus="s">-->
<!--                        <tr>-->
<!--                            <td><input type="checkbox" name="id" value="" class="content_Cb"></td>-->
<!--                            <td>${s.count}</td>-->
<!--                            <td>${topic.title}</td>-->
<!--                            <td>${topic.createBy}</td>-->
<!--                            <td>${topic.createTime}</td>-->
<!--                            <td>${topic.status}</td>-->

<!--                            <td>-->
<!--                                <a class="btn btn-default btn-sm" href="admin/top.do?id=${topic.id}">置顶</a>&nbsp;-->
<!--                                <a class="btn btn-default btn-sm" href="admin/essence.do?id=${topic.id}">加精</a>&nbsp;-->
<!--                                <a class="btn btn-default btn-sm" href="admin/deleteTopicById.do?id=${topic.id}">删除</a>-->
<!--                            </td>-->
<!--                        </tr>-->
<!--                    </c:forEach>-->
                    <tr th:each="topic:${topicsByCondition}" >
                        <td><input type="checkbox" name="id" th:value="${topic.id}" class="content_Cb"></td>
                        <td th:text="${topicStat.index+1}"></td>
                        <td th:text="${topic.title}"></td>
                        <td th:text="${topic.createBy}"></td>
                        <td th:text="${topic.createTime}"></td>
                        <td th:text="${topic.status}"></td>
                        <td>
                            <!--<a class="btn btn-default btn-sm" th:href="|admin/top?id=${topic.id}|">置顶</a>&nbsp;-->
                            <a class="btn btn-default btn-sm" th:th:onclick="|javascript:setTop('${topic.id}')|">置顶</a>
                            <a class="btn btn-default btn-sm" th:th:onclick="|javascript:setEssence('${topic.id}')|">加精</a>

                            <!--<a class="btn btn-default btn-sm" th:href="|admin/essence?id=${topic.id}|">加精</a>&nbsp;-->
                            <!--<a class="btn btn-default btn-sm" th:href="|admin/deleteTopicById?id=${topic.id}|">删除</a>-->
                            <a class="btn btn-default btn-sm" th:th:onclick="|javascript:deleteTopic('${topic.id}')|">删除</a>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </div>
</div>

<div class="modal-footer" style="margin-top: 10px">
    <div align="center"><a>Powered by 斌斌家豪涛顺 &nbsp;&nbsp;联系我们</a></div>
</div>
<script type="text/javascript">
</script>

</body>

</html>
